using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq.Dynamic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Dimok.Images;
using Data.LINQ;
using ABC = WebSupergoo.ABCpdf6;

namespace aloaners.Manager.Verification {
	public partial class DocsForDealer : Page {
		public string DocsPath { get { return Request["Path"] + ""; } }
		private void Page_Load(object sender, System.EventArgs e) {
			var loanID = Dimok.User.UserObject.applIDParam;
			var path = DocsPath;
			var docIDs =  (Request["Docs"] ?? "").Split(',').Where(s=>s!="").Select(s=>int.Parse(s)).ToArray();
			if (path == "" || docIDs.Length == 0) return;
			var mb = new MotoBank();
			List<byte[]> pdfFiles = new List<byte[]>();
			foreach (var docID in docIDs) {
				var rowView = mb.ts_Document_Views.Single(r => r.ID == docID);
				var viewName = rowView.View;
				pdfFiles.AddRange(Dimok.ABCPdf.FillForm(Server.MapPath(rowView.Path), mb, "SELECT * FROM " + viewName + " WHERE LoanID={0}", loanID));
			}
			#region Send Response
			Response.AddHeader("content-disposition", "attachment; filename=DocsFor_"+loanID+".pdf");
			Response.ContentType = Enums.GetContentType(pdfFiles[0]);
			Response.Buffer = false;
			Response.BinaryWrite(Dimok.ABCPdf.CombinePDF(pdfFiles.ToArray()));
			Response.End();
			#endregion
		}
	}
}